package com.yr.ryjk.focus.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.common.Pagination;
import com.yr.ryjk.doctor.dao.DoctorMapper;
import com.yr.ryjk.doctor.entity.Doctor;
import com.yr.ryjk.doctor.entity.FilterParam;
import com.yr.ryjk.focus.dao.FocusMapper;
import com.yr.ryjk.focus.entity.Focus;
import com.yr.ryjk.focus.entity.FocusVO2;
import com.yr.ryjk.focus.entity.FocusVOCensus;
import com.yr.ryjk.redis.JedisUtil;
import com.yr.ryjk.user.dao.UserMapper;
import com.yr.ryjk.user.entity.User;
import com.yr.ryjk.util.IDGenerator;
import com.yr.ryjk.util.JSONUtil;

@Service
public class FocusService implements IFocusService
{
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private FocusMapper focusMapper;
	@Autowired
	private DoctorMapper doctorMapper;
	
	@Override
	public Map<String, Object> updateToggle(String phone, int action, String token) {
		if(action==0){
			focusMapper.deleteByDocPhone(phone);
		}else if(action==1){
			Focus focus=new Focus();
			focus.setFocusId(IDGenerator.gen32UUID());
			focus.setFocusTime(new Date());
			
			String phoneInRedis=JedisUtil.getPhoneByToken(token);
			if(phoneInRedis!=null){
				User user=userMapper.selectUserByPhone(phoneInRedis);
				focus.setUserId(user.getId());
			}
			
			Doctor doctor=doctorMapper.selectByPhone(phone);
			focus.setDocId(doctor.getId());
			
			focusMapper.insert(focus);			
		}
		return JSONUtil.getJSON(ErrorType.SUCCESS); 
	}

	@Override
	public Map<String, Object> getListOfPage(int pageNo, int pageSize, String token,FilterParam filterParam) {
		String phone=JedisUtil.getPhoneByToken(token);
		User user=userMapper.selectUserByPhone(phone);
		
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("start", (pageNo-1)*pageSize);
		params.put("limit", pageSize);
		params.put("userId", user.getId());
		params.put("hospitalLevel", filterParam.getHospitalLevel());
		params.put("departmentId", filterParam.getDepartmentId());
		params.put("dutyId", filterParam.getDutyId());
		
		List<FocusVOCensus> list=focusMapper.selectByUserId(params);
		int count=focusMapper.selectCountByUserId(params);
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("list", list);
		map.put("coin", user.getCoin());
		map.put("pagination", new Pagination(pageSize,pageNo,count));
		return map;
	}

	@Override
	public Map<String, Object> getListOfFan(int pageNo, int pageSize,
			String token) {
		String phone=JedisUtil.getPhoneByToken(token);    
		Doctor doctor=doctorMapper.selectByPhone(phone);
		List<FocusVO2> list=focusMapper.selectByDocId((pageNo-1)*pageSize,pageSize,doctor.getId());		
		
		int count=focusMapper.selectCountOfFan(doctor.getId());
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("list", list);
		map.put("pagination", new Pagination(pageSize,pageNo,count));
		
		return map;
	}

}
